<script lang="ts" setup>
import { useVbenModal,z } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { postModules, putModules } from '#/api/portal/modules';
import { $t } from '@vben/locales';
const [Form, FormApi] = useVbenForm({
  handleSubmit: onSubmit,
  showDefaultActions: false,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      defaultValue: '',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'name',
      label: $t('admin.modelName'),
      rules: 'required',
    },
    {
      component: 'Input',
      defaultValue: '',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      dependencies: {
        disabled: (values) => {
          return !!values.id || values.type !== 'module';
        },
        trigger: (values) => {
          if (values.type !== 'module') {
            values.code = values.code;
          }
        },
        triggerFields: ['code', 'id', 'type', 'name'],
      },
      fieldName: 'code',
      label: $t('admin.identifier'),
      rules: z
    .string()
    .min(1, { message: $t('admin.identifierRequired') })
    .regex(/^[A-Za-z0-9_]+$/, { message: $t('admin.identifierFormat') }), // 更新的正则验证
    },
    {
      component: 'Textarea',
      defaultValue: '',

      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'description',
      label: $t('page.common.Description'),
    },
    {
      component: 'Select',
      defaultValue: 'module',
      dependencies: {
        disabled: (value) => {
          return !!value.id;
        },
        triggerFields: ['id', 'type'],
      },
      componentProps: {
        class: 'w-full',
        options: [
          {
            value: 'module',
            label: $t('admin.module'),
          },
          {
            value: 'parameter',
            label: $t('admin.parameter'),
          },
          {
            value: 'spec',
            label: $t('admin.specification'),
          },
        ],
      },
      fieldName: 'type',
      label: $t('page.common.Category'),
      rules: 'required',
    },
    {
      component: 'Switch',
      defaultValue: 1,
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: $t('page.common.Yes'),
        unCheckedChildren: $t('page.common.No'),
      },

      fieldName: 'status',
      label: $t('page.common.Status'),
      rules: 'required',
    },
    {
      component: 'Switch',
      defaultValue: 0,
      dependencies: {
        show: (value) => {
          return value.type === 'module';
        },
        triggerFields: ['type'],
      },
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: $t('page.common.Yes'),
        unCheckedChildren: $t('page.common.No'),
      },
      fieldName: 'is_menu',
      label: $t('admin.generateMenu'),
    },
  ],
});

const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onConfirm: async () => {
    await FormApi.validateAndSubmitForm();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: any = formModalApi.getData();
      if (values) {
        FormApi.setValues(values);
      }
    }
  },
});

async function onSubmit(values: any) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  if (values.id) {
    const { msg } = await putModules(values);
    showMsg(msg);
  } else {
    const { msg } = await postModules(values);
    showMsg(msg);
  }
}
</script>
<template>
  <Modal class="w-3/5"> <Form /> </Modal>
</template>

<style lang="scss" scoped></style>
